#coding:utf-8


_bin = ['0000', '0001', '0010', '0011',
        '0100', '0101', '0110', '0111',
        '1000', '1001', '1010', '1011',
        '1100', '1101', '1110', '1111',]

#车台校验和算法
def getchecksum(data):
    """ getchecksum_vehicle38_str(data)==> int\n
        
        Get the Checksum of data between Vehicle and Device,\n
        Used to send data or check received data.\n
        
    """
    checksum = 0
    for number in data:
        checksum += number
        if checksum > 0xff:
            checksum -= 0xff
    if checksum == 0: checksum = 0x7f
    return checksum

#车台末尾校验码(数字字符串)
def getchecksum_buss(data):
    """接收业务数据字符串,返回计算得到的校验和字符串"""
    checksum = 0
    for number in data:
        checksum += number
        if checksum > 0xff:
            checksum -= 0xff
    if checksum >= 0x80: checksum -= 0x80
    if checksum == 0x00: checksum = 0x7f
    return checksum

# 添加7e头尾
def add7e(data):
    if not 0x7e in data:
        return [0x7e]+data+[0x7e]
    else:
        write_event('要发送的数据中包含了"7e"项:%s'%data)
        return data

# 除掉7e头尾
def remove7e(data):
    assert isinstance(data, list)
    if (data.count(0x7e)%2) != 0:
        """异常数据"""
    else:
        datas = []
        _from = _to = 0
        for i in range(data.count(0x7e)/2):
            if data[_from] != 0x7e:
                """异常数据"""
            else:
                _to = data.index(0x7e, _from+1)
                datas.append(data[_from + 1 : _to])
                _from = _to + 1
        return datas
        
#ASCII码转义为16进制
def asc2data(asc_str):
    data = []
    for i in asc_str:
        data.append(ord(i))
    return data
        
#16进制转化为ASCII码
def data2asc(hexdata):
    asc_str = ""
    for i in hexdata:
        asc_str += chr(i)
    return asc_str.strip()

#16进制转化为Unicode字符
def data2unicode(data):
    hexdata = data2hex(data)
    return hexdata.decode('gbk')
    
#16进制转化为Unicode字符
def unicode2data(unistr):
    hexdata = unistr.encode('gbk')
    return hex2data(hexdata)


# 数据转化为数字
def data2num(data):
    num = 0
    m = len(data)
    for i in range(m):
        num = (256**(m-i-1))*data[i] + num
    return num

# 数字转化为数组
def num2data(num):
    data = []
    while True:
        data.insert(0, num%256)
        num  = num / 256
        if num == 0:
            break
    return data


# 从一个数字中抽取一部分
def extract(value, begin, end):
    de = value >> (7- end)
    return de & (2**(end-begin+1)-1)

# 将一个值插入到一个数字中
def insert(value, inserted, begin, end):
    inserted = inserted *(2**(7-end))
    de = 0xff -2**(8-begin) + 2**(7-end)
    return value & de | inserted



# 对一个序列的包装
def pickUps(data):
    if data is None: data=[]
    if isinstance(data, (str, unicode)):
        data = str2data(data)
    assert isinstance(data, list), "list or string:%s"%data
    class _Data:
        def __init__(self, data=[]):
            self._data = data
            self._cursor = 0
        def get(self, nums = 1):
            kids = self._data[self._cursor:self._cursor+nums]
            self._cursor = self._cursor + nums
            if len(kids) == 1:
                return kids[0]
            else:
                return kids
        def waste(self, nums = 1):
            self._cursor = self._cursor + nums
            return self
        def set(self, *args):
            for value in args:
                if isinstance(value, list):
                    self._data = self._data + value
                else:
                    self._data.append(value)   
        def out(self):
            return self._data
    return _Data(data)

#对一个字节的包装  
def pickUp(value = 0x0):
    assert isinstance(value, int)
    assert value >= 0x0 and value <= 0xff
    class _Value:
        def __init__(self, value):
            self.value = value
        def get(self, begin, end):
            return extract(self.value, begin, end)
        def set(self, value, begin, end):
            self.value = insert(self.value, value, begin, end)
        def out(self):
            return self.value
    return _Value(value)

# 二进制字符串转为文本字符串,显示用
def hex2str(hexstr):
    l = [character.encode('hex') for character in hexstr]
    return ' '.join(l)

# 二进制字符串转为数据序列,操作用
def hex2data(hexstr):
    return [ord(character) for character in hexstr]
    
# 文本字符串转为数据序列,操作用
def str2data(logstr):
    l = logstr.split(' ')
    return [int(i, 16) for i in l]

# 数据序列转为文本字符串,显示用
def data2str(data):
    logstr = ''
    for i in data:
        logstr += '%02x '%i
    return logstr.strip()

# 文本字符串转为二进制字符串,发送/接收数据用
def str2hex(logstr):
    l = logstr.split(" ")
    s = ''
    for character in l:
        s += character.decode('hex')
    return s

# 数据序列转为二进制字符串,发送/接收数据用
def data2hex(data):
    s= ''
    for number in data:
        s += chr(number)
    return s

# 转义字符,7e转为7d+02, 7d转为7d+01
def transform(data):
    l = []
    for number in data:
        if number == 0x7d:
            l += [0x7d, 0x01]
        elif number == 0x7e:
            l += [0x7d, 0x02]
        else:
            l.append(number)
    return l

# 逆转义字符,7d+01转为7d,7d+02转为7e
def untransform(data):
    l = []
    has7d = False
    for number in data:
        if number == 0x7d:
            has7d = True
        else:
            if has7d:
                if number == 0x01:
                    l.append(0x7d)
                elif number == 0x02:
                    l.append(0x7e)
                else:
                    """错误,单独的0x7d存在"""
                    l.append(0x7d)
                    l.append(number)
            else:
                l.append(number)
            has7d = False
    if has7d:
        """错误,单独的0x7d存在"""
    return l
        

def getKey(dict, value):
    for i in dict.items():
        if i[1] == value:
            return i[0]
    else:
        return None

#数据x转八
def code_x_to_8(data, code):
    de = ''
    for i in data:
        de += (_bin[i/16] + _bin[i%16])[8-code:]
    if not de: return []
    de = de.rstrip('0')
    lst = []
    for i in range(len(de) / 8 + 1):
        lst.append(de[i * 8 :i * 8 + 8])
        
    #最后一个值需要补0
    lst[-1] = (lst[-1] + '0' * 8 )[:8]
        
    return [int(i,2) for i in lst]
    
#数据八转X
def code_8_to_x(data, code):
    de = ''
    for i in data:
        de += _bin[i/16] + _bin[i%16]
    if not de: return []
    lst = []
    for i in range(len(de) / code + 1):
        lst.append(de[i * code :i * code + code])
    
    #最后一个值需要补0
    lst[-1] = (lst[-1] + '0' * code )[:code]
    return [int(i, 2) for i in lst]


if __name__ == '__main__':
    c = "21 02 42 00 15 00 09 05 00 00 1e 17 c4 70 79 ae c7 e1 52 91 e1 85 e0 0c b9 ae 36 74 23 ac 8d 69 e6 9a 78 a6 e6 93 34 24 a7 62 9a 2a 4e d4 00 eb 76 31 e7 67 14 f4 c9 6f 5a 8c 74 a7 c6 71 c9 a9 48 45 5b 7f be d9 39 e6 ac 34 49 2c 32 db cc 37 c3 28 c3 a1 ef 55 a0 c0 95 b6 f4 cd 5b 06 a9 97 cc 25 8d ad 9d 8c 02 de c2 d9 2d a2 fe ea d5 b4 35 0a d3 d2 a6 c0 68 5a 0e 73 57 45 53 b1 ab 75 2c d2 23 8f 15 e0 d1 12 9a 7e a0 be b7 52 7f 3a f7 4b a7 d9 13 3f 07 15 e1 af 13 8b 1b b9 08 c2 99 df f9 d2 80 49 d8 e3 9f fd 61 e3 bd 5b b6 aa 4c c0 c8 70 7b d5 eb 4e d5 bd 8e 63 37 55 5f 33 c5 fa 04 4c 3a dc f4 f5 e2 be a9 b2 18 b3 80 7a 46 2b e6 26 50 ff 00 11 bc 3c 9d 73 38 fc 78 af a8 62 18 89 07 fb 35 20 3a 8a 28 a0 18 51 45 14 84 14 51 45 32 c2 8a 28 aa 2d 05 72 9a 9a 67 c6 b1 37 a0 5a ea eb 9a d4 c6 3c 57 09 e7 27 14 0c d1 b8 20 02 7a 54 04 8d 95 2e ad 13 3c 18 8f 93 fc 55 49 92 51 81 8e 2a 46 87 6d dc 30 2b c4 ff 00 6f 0e 3e 15 e9 fc ff 00 cb 73 fc 85 7b 6f 41 5c 87 c5 ff 00 0a d9 7c 40 d0 ed f4 bd 49 71 0c 0f be 85 1e a1 23 5f c1 a0 c7 f0 fb 41 8b 8f f8 f1 88 ff 00 e3 82 b7 f4 20 7e d3 27 1c 6d ac 5d 24 24 1a 25 b5 84 60 ed b7 8c 44 bf 41 c5 74 3a 3b ed 8c 82 dc 0a 6c 85 a1 f3 67 c2 c9 56 f3 f6 98 f1 7c 56 eb 88 c5 a2 61 7d f7 1a f6 19 20 fd fb fa e6 b8 7f 07 fc 3d d4 7c 2f f1 63 5b f1 74 92 07 83 50 40 89 93 d3 9c d7 73 14 e1 9d d8 9e f4 84 dd c8 4c 4c 29 93 8f 96 ad 97 56 e9 50 4e 3f 74 68 1d cf 34 f8 ed 8f ed 0f 0b fb dd 7f 4a f4 18 87 fa 34 07 fe 99 8a f3 ff 00 8e a7 fe 26 9e 16 1e 97 5f d2 bd 0e 30 05 9c 1f f5 cc 51 61 0d db 9a 5f bb 4e 14 8d 4b 61 a4 33 1f 36 ea 87 c8 8f 92 57 24 d4 f4 d3 4e e3 e5 45 46 d3 ec e4 3b a5 81 59 a9 3e c1 6c 9f ea e2 0a 6a de 71 48 39 a0 2c 43 0c 45 79 12 30 35 2c 6d a8 2f 23 53 9e 9d 4e 8f ad 04 d8 b9 f0 ae eb 51 96 ee f6 2b db b7 bb db c8 77 eb d6 bb f3 5e 7b f0 9c 83 ab 6a 58 1d 07 f5 af 42 c5 31 08 7a 54 62 a5 a6 e2 b5 20 29 d4 98 a7 2f 5a 83 41 71 45 2d 14 84 14 e1 4c a7 8a ab 0c 75 26 69 68 c5 21 f3 09 45 2d 25 48 ee 31 a9 b9 a7 75 a6 d0 1c c2 2f de ac 9d 73 fe 3e eb 59 3a d6 56 bf fe bc 53 26 e6 75 44 dd 69 ed 51 35 16 0e 62 27 eb 51 93 4e 7a 84 9a 62 6c 52 69 b4 99 a6 ee a1 8a e5 6d 09 6d df c3 ba 28 49 a3 3f f1 2d 83 bf fb 02 b6 22 89 12 1f dd f2 be b5 5f c2 fe 0c b1 b8 d0 b4 2b e4 9a 48 17 fb 3e 0d d1 28 e1 be 41 54 62 f0 1f 88 6f 65 b8 fb 4f 89 6e b4 b8 0b 10 90 db 61 86 2b 55 33 1b 16 2e 50 73 b7 9a cc 68 b9 ab 29 f0 ae fe 39 37 2f 8f 75 9c 7f 77 62 d4 36 de 10 f1 55 a2 cb e7 4e b3 22 fd d6 df cb 56 ca a4 51 12 89 81 ae a6 70 3a 62 bd 2c 0f 2f c0 12 73 ff 00 2c 6b cf 7c 55 a5 6b fa 7e 97 3d d5 c4 30 88 c2 fd ed f5 e8 3a c0 d9 f0 d6 5c 71 9b 35 3f ca b2 9c af aa 0a 6a c7 8f ea ca 17 46 ba 22 bb 6f 86 48 07 84 e1 6c 75 73 5c 5d d8 32 f8 7a 64 1f 7c af 15 dd 7c 3c 4f 2f c3 10 c7 d0 ee 35 ce d9 b1 b9 3d 44 9c 9a 2e 1b 9a 74 1e b4 8d 09 85 49 4d a0 1a 06 06 9e 9c 8a 8d 8d 08 d4 21 91 47 c4 86 ac 8a ac 9f 7e ac 0a 09 27 5e 94 e5 35 0a b5 20 7a 2c 33 4e d6 4a b3 e7 6d 61 59 56 f2 62 ac ab e4 d4 b3 54 2f 88 e7 f2 b4 0b e9 c7 f0 47 9a f3 2b d4 6f f8 42 5a e5 fe 62 cc d5 dd f8 d2 74 3e 13 d6 36 71 b6 de bc fb 59 9d 87 c3 e0 bb c8 5c 64 0a 84 89 3c e6 2e a7 9e f5 a5 69 59 30 b7 15 a9 6d d2 b6 32 1d 64 0c 9f 15 fc 3c 80 f4 98 1f d2 be 9d 1d 05 7c d1 a0 0f 33 e3 06 8c 07 f0 b2 ff 00 2a fa 62 8b 00 51 45 14 86 14 51 45 3b 15 60 a2 8a 29 00 51 45 15 76 10 56 26 a9 1b ff 00 6c 43 30 fb a2 b6 eb 23 50 77 fe d3 44 da 9e 5a f3 d7 93 4a c5 a2 c3 32 9e a6 a2 6d a6 96 50 af ca a6 da 8f c9 f3 3e 5d c5 6a 47 72 ac 87 e6 a8 a4 5c d4 af 6a ea dc b9 34 c9 ad dd 50 1d ed 41 17 b8 d5 40 ab 81 52 59 39 3b 86 78 15 17 95 22 af 7a a1 61 76 ce d3 22 e7 03 ad 01 63 4b 54 5d f0 2e e3 b8 0e 82 b0 d2 dd 0e 4b 0a bf 24 ad b3 18 aa bb b0 29 58 65 45 88 a1 3e 94 c9 fe e1 15 2b 31 aa b7 0d f2 d3 0b 9e 73 f1 db fe 43 3e 17 5f 5b af e9 5e 8a a7 fd 12 0f fa e6 2b cf 3e 3a a3 2f 88 bc 27 13 63 3f 69 ce 3f e0"
    y = data2str(str2data(c))
    if not "!" in y:
        print y
